using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SQLite;
using System.Data;
using System.IO;



namespace FetionConvert
{
    class FetionCovertor
    {
        private string CreateJsonParameters(DataTable dt)
        {

            StringBuilder JsonString = new StringBuilder();
            //Exception Handling        
            if (dt != null && dt.Rows.Count > 0)
            {
                JsonString.Append("{ ");
                JsonString.Append("\"Head\":[ ");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    JsonString.Append("{ ");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        if (j < dt.Columns.Count - 1)
                        {
                            JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\",");
                        }
                        else if (j == dt.Columns.Count - 1)
                        {
                            JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\"");
                        }
                    }
                    /*end Of String*/
                    if (i == dt.Rows.Count - 1)
                    {
                        JsonString.Append("} ");
                    }
                    else
                    {
                        JsonString.Append("}, ");
                    }
                }
                JsonString.Append("]}");
                return JsonString.ToString();
            }
            else
            {
                return null;
            }
        }
        private DataTable getDataTable(String historyPath, String key)
        {
            SQLiteConnection conn = new SQLiteConnection("Data Source=" + historyPath + ";Version=3;Password=" + key);
            SQLiteDataAdapter sda = new SQLiteDataAdapter();
            DataSet ds = new DataSet();
            conn.Open();
            sda.SelectCommand = new SQLiteCommand("select Message.ReceiverName,MessageBody.SenderName,MessageBody.Body,MessageBody.Time from Message,MessageBody where Message.MessageID=MessageBody.ID order by MessageBody.Time", conn);
            sda.Fill(ds, "Player");
            DataTable dt = ds.Tables[0];
            sda.Dispose();
            conn.Close();
            return dt;
        }
        private void save2file(String jsonStr, String destPath)
        {
            FileInfo f = new FileInfo(destPath);
            StreamWriter w = f.CreateText();
            w.Write(jsonStr);
            w.Close();
        }
        static void Main(string[] args)
        {
            FetionCovertor fc = new FetionCovertor();
            if (args.Length != 3)
            {
                fc.usage();
            }
            else
            {
                String sourceFile=args[0];
                String destFile=args[1];
                String key = args[2];
                Console.WriteLine("Source File:         "+sourceFile);
                Console.WriteLine("Destination File:    "+destFile);
                Console.WriteLine("Key:                 "+key);

                DataTable dt = fc.getDataTable(sourceFile, key);
                String json = fc.CreateJsonParameters(dt);
                //Console.WriteLine(json);
                fc.save2file(json, destFile);
            }



        }

        private void usage()
        {
            Console.WriteLine("Usage:");
            Console.WriteLine("FetionConvertor source destination key");
            Console.WriteLine("e.g. FetionConvertor d:/history.dat d:/message.txt 644843409");
        }
    }
}
